package com.kr.aia.powerrp;

import com.kr.aia.powerrp.closing.ClosingMain;
import com.kr.aia.powerrp.common.MainMovieClipListView;
import com.kr.aia.powerrp.db.MySQLiteHelper;
import com.kr.aia.powerrp.R;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.util.LruCache;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;

public class ManualMain extends Activity implements OnClickListener{
    private ImageButton mcList = null;
    private ImageButton btn_ta_main = null;
    private ImageButton btn_rp_main = null;
    private ImageButton btn_closing_main = null;
    private ImageButton btn_manual_main = null;
    
        
    MainMovieClipListView mcv;
    
    MySQLiteHelper dbHelper;
    SQLiteDatabase database;
    ProgressDialog initDialog;
    
    private LruCache<String, Bitmap> mMemoryCache;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        
        
        //최초 실행시 DB Table 생성
        initDialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);
        new dbCreationTask().execute(this);
        
        
        btn_ta_main = (ImageButton)findViewById(R.id.btn_ta_main);
        btn_ta_main.setOnClickListener(this);
        
        btn_rp_main = (ImageButton)findViewById(R.id.btn_rp_main);
        btn_rp_main.setOnClickListener(this);
        
        btn_closing_main = (ImageButton)findViewById(R.id.btn_closing_main);
        btn_closing_main.setOnClickListener(this);
        
        btn_manual_main = (ImageButton)findViewById(R.id.btn_manual_main);
        btn_manual_main.setOnClickListener(this);
        
        
        
        mcList = (ImageButton)findViewById(R.id.btn_view_movieclip);
        mcList.setOnClickListener(this);
        
        
        // 클릭 효과 세팅
        /*btn_ta_main.setOnTouchListener(new commonOnTouchListener());
        btn_rp_main.setOnTouchListener(new commonOnTouchListener());
        btn_closing_main.setOnTouchListener(new commonOnTouchListener());
        btn_manual_main.setOnTouchListener(new commonOnTouchListener());
        mcList.setOnTouchListener(new commonOnTouchListener());*/
        
        
        final int memClass = ((ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();

        // Use 1/8th of the available memory for this memory cache.
        final int cacheSize = 1024 * 1024 * memClass / 8;

        mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
            /*
            @Override
            protected int sizeOf(String key, Bitmap bitmap) {
                // The cache size will be measured in bytes rather than number of items.
                return bitmap.getByteCount();
            }
            */
        };
    }
    
    public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
        if (getBitmapFromMemCache(key) == null) {
            mMemoryCache.put(key, bitmap);
        }
    }

    public Bitmap getBitmapFromMemCache(String key) {
        return mMemoryCache.get(key);
    }
    
    
    @Override
    public void onClick(View v){
    	switch(v.getId()){
    		case R.id.btn_view_movieclip:
    			AlertDialog.Builder builder = new AlertDialog.Builder(this);
    			
    		    mcv = new MainMovieClipListView(this, "");
    			View mcvView = mcv.openMovieClipListView();
    			
		   		//.setIcon(R.drawable.icon_sja)		// icon 지정 후 Builder 객체 리턴
    			builder.setTitle("동영상 목록")				// title 지정
		   		//.setMessage("메시지 영역")			// 메시지 text 지정
		   		.setView(mcvView)	// 커스텀 뷰 지정
		   		// AlertDialog의 버튼 생성 & 버튼 클릭 이밴트에 대한 Listener 객체 생성/등록 
		   		.setPositiveButton("동영상 보기", new positiveButtonClickListener())
		   		.setNegativeButton("닫기", new DialogInterface.OnClickListener(){
		   			@Override
					public void onClick(DialogInterface dialog, int which) {
		   				dialog.dismiss();
		   			}
		   		});
    			builder.show(); // 지금까지 설정한 dialog를 화면에 보이고 AlertView객체를 return
	    		
    			break;
    		case R.id.btn_ta_main:
    			//Intent ta_intent = new Intent(this, TAMain.class);
    			//startActivity(ta_intent);
    			break;
    		case R.id.btn_rp_main:
    			//Intent rp_intent = new Intent(this, RPMain.class);
    			//startActivity(rp_intent);
    			break;
    		case R.id.btn_closing_main:
    			Intent closing_intent = new Intent(this, ClosingMain.class);
    			startActivity(closing_intent);
    			break;
    		case R.id.btn_manual_main:
    			Intent manual_intent = new Intent(this, ManualMain.class);
    			startActivity(manual_intent);
    		default:
    			break;
    	}
    }
    
	public class positiveButtonClickListener implements DialogInterface.OnClickListener{


		@Override
		public void onClick(DialogInterface dialog, int which) {
			//dialog.dismiss();
			//Log.e("PRP", "----------------- " + intentUri.toString());
			if(mcv.getIntentUri() != null){
				Intent intent = new Intent();
	            intent.setAction(Intent.ACTION_VIEW);
	            intent.setDataAndType(mcv.getIntentUri(), "video/mp4");
	            startActivity(intent);
			}
		}
	};
    
    private class dbCreationTask extends AsyncTask<Context, Void, Void> {
		@Override
		protected Void doInBackground(Context... arg) {
			// TODO Auto-generated method stub
			dbHelper = new MySQLiteHelper(arg[0]);
	        database = dbHelper.getWritableDatabase();
	        dbHelper.createDatabase(database);
			return null;
		}
		
		@Override
		protected void onPostExecute(Void v) {
			dbHelper.close();
			if(initDialog.isShowing())
        		initDialog.dismiss();
		}
    }
    

    /*
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        menu.add(0, 0, 0, "StartRecording");
        menu.add(0, 1, 0, "StopRecording");
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
        case 0:
            try {
            	preview.startRecording();
            } catch (Exception e) {
                String message = e.getMessage();
                Log.i(null, "Problem Start"+message);
                //mrec.release();
                preview.releaseMediaRecorder();
            }
            break;

        case 1: //GoToAllNotes
//            mrec.stop();
//            mrec.release();
//            mrec = null;
        	preview.stopRecording();
            break;

        default:
            break;
        }
        return super.onOptionsItemSelected(item);
    }
    */

}